using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DesktopWidgetApp.Shared;

namespace DesktopWidgetApp.Core.Data
{
    /// <summary>
    /// SQLite任务仓储实现
    /// </summary>
    public class SqliteTaskRepository : SqliteRepositoryBase<TaskItem, Guid>, ITaskRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="databasePath">数据库路径</param>
        public SqliteTaskRepository(string databasePath) : base(databasePath)
        {}

        /// <summary>
        /// 根据状态获取任务
        /// </summary>
        /// <param name="status">任务状态</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetByStatusAsync(TaskStatus status)
        {
            return await _database.Table<TaskItem>()
                .Where(t => t.Status == status)
                .ToListAsync();
        }

        /// <summary>
        /// 根据优先级获取任务
        /// </summary>
        /// <param name="priority">优先级</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetByPriorityAsync(Priority priority)
        {
            return await _database.Table<TaskItem>()
                .Where(t => t.Priority == priority)
                .ToListAsync();
        }

        /// <summary>
        /// 获取截止日期前的任务
        /// </summary>
        /// <param name="dueDate">截止日期</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetByDueDateAsync(DateTime dueDate)
        {
            return await _database.Table<TaskItem>()
                .Where(t => t.DueTime.HasValue && t.DueTime <= dueDate)
                .ToListAsync();
        }

        /// <summary>
        /// 根据标签获取任务
        /// </summary>
        /// <param name="tag">标签</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetByTagAsync(string tag)
        {
            // SQLite-net不直接支持数组查询，这里我们使用包含查询
            return await _database.Table<TaskItem>()
                .Where(t => t.Tags != null && t.Tags.Contains(tag))
                .ToListAsync();
        }
    }
}